package com.zhz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhz.domain.Config;
import com.zhz.model.R;
import com.zhz.service.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author ：zhz
 * @date ：Created in 2021/01/01
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
@RestController
@RequestMapping("/configs")
@Api(tags = "后台参数配置")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @GetMapping
    @ApiOperation(value = "配置信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "配置规则代码"),
            @ApiImplicitParam(name = "current", value = "当前页数"),
            @ApiImplicitParam(name = "size", value = "每页条数"),
            @ApiImplicitParam(name = "name", value = "配置规则名称"),
            @ApiImplicitParam(name = "type", value = "配置规则类型,CNY/SYSTEM")
    })
    @PreAuthorize("hasAuthority('config__query')")
    public R<Page<Config>> findByPage(@ApiIgnore Page<Config> page,String type, String code,String name) {
        return R.ok(configService.findByPage(page, type,code,name));
    }

    @GetMapping
    @ApiOperation(value = "新增配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config", value = "配置参数")
    })
    @PreAuthorize("hasAuthority('config_create')")
    public R addConfig(@RequestBody @Validated Config config) {
        return configService.save(config)?R.ok():R.fail("新增失败");
    }
    @GetMapping
    @ApiOperation(value = "修改配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config", value = "配置参数")
    })
    @PreAuthorize("hasAuthority('config_update')")
    public R updateConfig(@RequestBody @Validated Config config) {
        return configService.updateById(config)?R.ok():R.fail("修改失败");
    }
}
